package org.magento.pages;

import org.magento.data.CustomerData;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

//This class describes admin/sales_orde page

public class OrdersBeckEnd extends Page {
	
	//Locators:
	@FindBy (id = "sales_order_grid_filter_billing_name")
	public WebElement textFieldCustomerNameSearch;
	
	@FindBy (id = "sales_order_grid_filter_status")
	public WebElement listOrderStatus;
	
	@FindBy (xpath = "/descendant::td[@class='filter-actions a-right']/button[@title = 'Reset Filter']")
	public WebElement buttonResetFilter;
	
	@FindBy (xpath = "/descendant::td[@class='filter-actions a-right']/button[@title = 'Search']")
	public WebElement buttonSearch;
	
	@FindBy (linkText = "View")
	public WebElement linkView;
	
	@FindBy (xpath = "/descendant::td[@class='form-buttons']/button")
	public WebElement buttonCreateNewOrder;
	
	@FindBy (id = "sales_order_grid_filter_real_order_id")
	public WebElement orderID;
	
	//Actions:
	
	//Edit order only one order on the page 
	
	public OrderViewBeckEnd editDisplayedOrder() throws Exception {
		linkView.click();
		return PageFactory.initElements(driver, OrderViewBeckEnd.class);
	}
	
	//Select and edit last created order by Default customer  with status pending
	public OrderViewBeckEnd editOrder(CustomerData customer) throws Exception {
		buttonResetFilter.click();
		waitForFiveSeconds();
		type(textFieldCustomerNameSearch, customer.creditCardName);
		select(listOrderStatus, "Pending");
		buttonSearch.click();
		waitForFiveSeconds();
		linkView.click();
		return PageFactory.initElements(driver, OrderViewBeckEnd.class);
	}
	
	//Create new order
	public CreateNewOrderBeckEnd  createNewOrder() {
		buttonCreateNewOrder.click();
		return PageFactory.initElements(driver, CreateNewOrderBeckEnd.class);
	}
	
	//Find order by ID from DB 
	public OrdersBeckEnd findByID(String orderIdFromDataBase) {
		type(orderID, orderIdFromDataBase);
		buttonSearch.click();
		return PageFactory.initElements(driver,OrdersBeckEnd.class);
	}
	
	//Assertions: 
	public boolean isOrderCanceled() {
		return driver.findElement(By.cssSelector("BODY")).getText().contains("The order has been cancelled.");
	}
	
	
	//Auto-generated:
	public OrdersBeckEnd(WebDriver driver) {
		super(driver);
	}

	@Override
	public void open() {
				
	}



}
